﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entetities;
using System.Data;

public partial class category : System.Web.UI.Page
{
    private SanPhamBLL bll;
    private NhaSanXuatBO nbll;
    private NhomBoLocBO nbl;
    private BoLocBO bl;
    public int cateID { get; set; }
    private int idNSX = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        bll = new SanPhamBLL();
        nbll = new NhaSanXuatBO();
        nbl = new NhomBoLocBO();
        bl = new BoLocBO();

        string id = Request.QueryString["catID"];
        string idnsx = Request.QueryString["nsx"];
        if (idnsx != null)
        {
            try
            {
                idNSX = int.Parse(idnsx);
            }
            catch (Exception)
            {
                return;
            }
        }
        if (id == null)
        {
            Response.Redirect("Default.aspx");
        }
        else
        {
            try
            {
                cateID = int.Parse(id);
            }
            catch (Exception)
            {
                Response.Redirect("Default.aspx");
            }
        }

        if (!IsPostBack)
        {
            DataTable dt = bll.getsanpham(cateID);
            DataListSP.DataSource = dt;
            DataListSP.DataBind();
            if (idNSX != -1)
            {
                DataList1.DataSource = nbll.getNhaSanXuatByID(idNSX);
                DataList1.DataBind();
            }
            else
            {
                DataList1.DataSource = nbll.getNhaSanXuatByNhom(cateID);
                DataList1.DataBind();
            }

            rptParent.DataSource = nbl.getNhomBoLoc(cateID);
            rptParent.DataBind();
        }
    }


    protected void ImageButton1_Command(object sender, CommandEventArgs e)
    {
        if (idNSX != -1)
        {
            DataList1.DataSource = nbll.getNhaSanXuatByID(idNSX);
            DataList1.DataBind();
            display();
        }
    }

    //loc san pham
    private void display()
    {
        string sql = null;
        string sql1 = null;
        int soluong = 0;
        List<BoLoc> listChon = new List<BoLoc>();
        foreach (StateItem i in ViewState.Values)
        {
            if (i != null)
            {
                BoLoc bo = (BoLoc)i.Value;
                if (bo != null)
                {
                    if (bo.Manhomboloc.ToString() != "")
                    {
                        sql += (" and Manhomboloc<>" + bo.Manhomboloc.ToString());
                        sql1 += (" or boloc.Maboloc = " + bo.Maboloc.ToString());
                        soluong++;
                        listChon.Add(bo);
                    }
                }
            }
        }

        if (sql == null)
        {
            rptParent.DataSource = bl.getBoLoc(cateID.ToString(), "");
            rptParent.DataBind();
        }
        else
        {
            rptParent.DataSource = bl.getBoLoc(cateID.ToString(), sql);
            rptParent.DataBind();
        }

            rptChon.DataSource = listChon;
            rptChon.DataBind();
            Response.Write(soluong);

        /////////////hien thi list san pham////////////////////
        if (sql1 != null)
        {

            string sql2 = sql1.Substring(3);
            if (idNSX != -1)
            {
                sql2 += " and Manhasanxuat = " + idNSX.ToString();
            }
            DataListSP.DataSource = bll.getListSanPham(sql2, soluong.ToString());
            DataListSP.DataBind();
            Response.Write(sql2);
        }
        else
        {
            if (idNSX == -1)
            {
                DataListSP.DataSource = bll.getsanpham(cateID);
                DataListSP.DataBind();
            }
            else
            {
                DataListSP.DataSource = bll.getsanpham(cateID, idNSX);
                DataListSP.DataBind();
            }
        }
    }

    protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //tìm điều khiển rptchild trong 1 item của rptParent
        Repeater rpt_Con = (Repeater)e.Item.FindControl("rptChild");
        if (rpt_Con != null)
        {
            HiddenField HiddenParentID = (HiddenField)e.Item.FindControl("HiddenParentID");//lay ID theo parentID
            if (HiddenParentID != null)
            {
                int manhomboloc = int.Parse(HiddenParentID.Value.ToString());
                rpt_Con.DataSource = bl.getBoLoc(manhomboloc);
                rpt_Con.DataBind();
            }
        }
    }

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        BoLoc bl1 = bl.geBoLocEntity(id);
        ViewState[bl1.Manhomboloc.ToString()] = bl1;
        display();
    }


    protected void Huy_Command(object sender, CommandEventArgs e)
    {
        string manhomboloc = e.CommandArgument.ToString();
        ViewState[manhomboloc] = null;
        display();
    }
}